package com.jackstraw.jack_base.datasource.jpa.conf;

import com.jackstraw.jack_base.datasource.jpa.base.dao.database.SimpleJpaDao;
import com.jackstraw.jack_base.datasource.jpa.base.dao.database.factory.BaseRepositoryFactoryBean;
import com.jackstraw.jack_base.util.SpringUtil;
import com.jackstraw.jack_base.util.condition.ConditionalOnHavingProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@Slf4j
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@EnableJpaRepositories(basePackages = {""},
        repositoryBaseClass = SimpleJpaDao.class,
        repositoryFactoryBeanClass = BaseRepositoryFactoryBean.class)
public class JpaConfig {


    public JpaConfig(ApplicationContext applicationContext) {
        SpringUtil.setApplicationContextJack(applicationContext);
    }

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "jack.datasource.jpa")
    @ConditionalOnHavingProperties(prefix = "jack.datasource", value = "jpa")
    public JpaProperties mybatisProperties() {
        return new JpaProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "jack.datasource.jpa.hibernate")
    @ConditionalOnHavingProperties(prefix = "jack.datasource", value = "jpa.hibernate")
    public HibernateProperties hibernateProperties() {
        return new HibernateProperties();
    }


}